package com.sgb.kjwl.view.ui.activity.leaseAsk.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sdk.mobile.manager.login.NoDoubleClickListener;
import com.sgb.capital.utils.DialogHelper;
import com.sgb.kjwl.R;
import com.sgb.kjwl.TitleImmerse;
import com.sgb.kjwl.databinding.FragmentAskLeaseFarm1Binding;
import com.sgb.kjwl.model.entity.AskLeaseFarmEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.utils.EditInputFilter;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.kjwl.viewmodel.machine.AskLeaseCommonModel;
import com.sgb.kjwl.viewmodel.machine.AskLeaseFarmModel2;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AskLeaseFarmFragment1 extends Fragment implements ItemInputTextView.OnWholeItemClickListener {
    private AskLeaseFarmModel2 viewModel;
    private AskLeaseCommonModel commonModel;
    private FragmentAskLeaseFarm1Binding binding;

    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private Calendar selectedDate = Calendar.getInstance();
    private Calendar selectedDate2 = Calendar.getInstance();

    private List<PickSelectEntity> classDatas;
    private List<List<PickSelectEntity>> typeDatas;
    private List<PickSelectEntity> driverDatas;
    private List<DictEntity3> allpayway = new ArrayList<>();//支付方式列表
    private List<UnitEntity> priceUnit = new ArrayList<>();//价格单位列表
    private List<PickSelectEntity> priceMatchUnitDatas = new ArrayList<>();
    private List<PickSelectEntity> leaseTimeDatas = new ArrayList<>();//租赁时长单位
    private String id = "";
    private String secondCategoryName = "";//二级分类，用于推荐标题拼接
    private OptionsPickerView  leaseTimeOpv;
    private String lat,lon;
    public void onAttach(Context context) {
        super.onAttach(context);
        id = getArguments().getString("id");
    }

    public static AskLeaseFarmFragment1 newInstance(String id) {
        AskLeaseFarmFragment1 frag = new AskLeaseFarmFragment1();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        frag.setArguments(bundle);
        return frag;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_ask_lease_farm_1, container, false);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        viewModel = new ViewModelProvider(getActivity()).get(AskLeaseFarmModel2.class);
        commonModel = new ViewModelProvider(this).get(AskLeaseCommonModel.class);
        recevierliveData();
        initView();
        initData();
    }


    private void initView() {
        String contact = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, "");
        String loginPhone = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
        binding.leaseUsername.setContent(contact);
        binding.itemTvPhone.setContent(loginPhone);


        classDatas = new ArrayList<>();
        typeDatas = new ArrayList<>();
        allpayway = new ArrayList<>();

        viewModel.getAddressModel();//请求省市区数据
        viewModel.getTypeModel();//数据类型
        viewModel.getAllPayWay();//付款方式
        if (!TextUtils.isEmpty(id)) {
            viewModel.getDetail(id);
        }
        //立即发布按钮
        binding.nextItem.btNextStep.setOnClickListener(v -> {
            if (checkInput()) {
                viewModel.selectFragLiveData.postValue(1);
            }
        });
        binding.nextItem.publicView.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (checkInput()) {
                    viewModel.addOrupdate(viewModel.entity);
                }
            }
        });
        binding.includeTime.tvTimeUnit.setOnClickListener(v -> {//租赁时长
            if (leaseTimeOpv != null) {
                leaseTimeOpv.show();
            }
        });
        binding.include.tvUse.setOnClickListener(v -> binding.include.itemTvTitleEdit.setEditContent(binding.include.tvTitleDefault.getText().toString()));
        binding.inLeaseNum.llMachineryNumType.setOnClickListener(v -> showMatchPriceUnitPop());
        binding.itvType.setOnWholeItemClickListener(this);
        binding.address.setOnWholeItemClickListener(this);
        binding.timeCourse.setOnWholeItemClickListener(this);
        binding.timeDeadline.setOnWholeItemClickListener(this);
        binding.paymentMethod.setOnWholeItemClickListener(this);
        binding.itemTvDriver.setOnWholeItemClickListener(this);
        binding.itemTvPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        binding.inLeaseNum.machineryNum.setFilters(new EditInputFilter[]{new EditInputFilter(9999999, 1, 0)});
        binding.includeTime.leaseTime.setFilters(new EditInputFilter[]{new EditInputFilter(9999999, 1, 0)});
        binding.inLeasePrice.leasePrice.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.address.setContentTextEnd();

        //   面议还是报价功能
        binding.inLeasePrice.rbPrice.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                binding.inLeasePrice.rbPrice.setButtonDrawable(R.mipmap.lease_pub_select);
                binding.inLeasePrice.layoutPrice.setVisibility(View.GONE);
                binding.inLeasePrice.rbPrice.setText("您已选择面议，再次点击退出面议");
            } else {
                binding.inLeasePrice.rbPrice.setButtonDrawable(R.mipmap.lease_pub_unselect);
                binding.inLeasePrice.layoutPrice.setVisibility(View.VISIBLE);
                binding.inLeasePrice.leasePrice.setText("");
                binding.inLeasePrice.leasePrice.setHint("请输入(必填)");
                binding.inLeasePrice.rbPrice.setText("面议");
            }
        });
        binding.itvBrand.onTextChange(s -> setDefaultTitle());
        binding.itvModel.onTextChange(s -> setDefaultTitle());
    }


    private void initData() {
        lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.SelectLATITUDE, "");
        lon = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.SelectLONGITUDE, "");
        //是否带操作员（0:是;1:否）
        driverDatas = new ArrayList<>();
        driverDatas.add(new PickSelectEntity("1", "是"));
        driverDatas.add(new PickSelectEntity("0", "否"));
        viewModel.entity.setHasOperator("1");
        binding.itemTvDriver.setContent("是");

        priceMatchUnitDatas = new ArrayList<>();
        priceMatchUnitDatas.add(new PickSelectEntity("0", "台"));
        priceMatchUnitDatas.add(new PickSelectEntity("1", "辆"));
        viewModel.entity.setRentalCountUnit("台");
        commonModel.getLeaseTimeUnit(6 + "");//获取租赁时长单位

    }

    /**
     * 设置沉浸状态栏下的title
     *
     * @param isImplement
     */
    protected void setTileImmerse(boolean isImplement) {
        if (isImplement) {
            TitleImmerse.assistActivity(getActivity().findViewById(android.R.id.content));

        }
    }

    private void recevierliveData() {
        viewModel.showLoad.observe(getActivity(), aBoolean -> {
            if (aBoolean) {
                DialogHelper.showProgressDialog(getActivity(), "加载中...", 0, false).setCanceledOnTouchOutside(false);
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        viewModel.initAddressDataLiveData.observe(getActivity(), data -> initAddressData(data));
        viewModel.msgLiveData.observe(getActivity(), data -> MToast.showToast(getActivity(), data));
        viewModel.showTypeViewLiveData.observe(getActivity(), data -> showTypeView(data));
        viewModel.showAllPayWayLiveData.observe(getActivity(), data -> showAllPayWay(data));
        viewModel.showDetailEntity.observe(getActivity(), data -> showDetail(data));
        binding.includeTime.tvTimeUnit.setOnClickListener(v -> {//租赁时长
            if (leaseTimeOpv != null) {
                leaseTimeOpv.show();
            }
        });
        commonModel.leaseTimeUnitLiveData.observe(getActivity(), t -> {
            leaseTimeDatas.addAll(t);
            showLeaseTime();
        });
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.itv_type://租赁类型
                if (classDatas.size() != 0) {
                    showClassTypePop();
                }
                break;
            case R.id.address:
                getLocationLL2();
                break;
            case R.id.time_course://使用时间(进场时间)
                showCourseTimePop();
                break;
            case R.id.time_deadline://报价截止时间
                if (TextUtils.isEmpty(binding.timeCourse.getContent())) {
                    MToast.showToast(getActivity(), "请选择使用时间");
                    return;
                } else {
                    showLimitTimePop();
                }
                break;
            case R.id.payment_method://付款方式选择
                showPaymentMethodPop();
                break;
            case R.id.item_tv_driver://选择是否自带操作员
                showDriverPicker();
                break;
            default:
                break;
        }
    }

    public void getLocationLL2() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SelectAddressActivity.class);
        intent.putExtra(SelectAddressActivity.TITLE, "工作地");
        intent.putExtra(SelectAddressActivity.HINT, "请输入地址");
        intent.putExtra(SelectAddressActivity.SAVE_TEXT, "确定");
        if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lon)) {
            intent.putExtra(SelectAddressActivity.LATITUDE, Double.parseDouble(lat));
            intent.putExtra(SelectAddressActivity.LONGTITUDE, Double.parseDouble(lon));
        }
        startActivityForResult(intent, 255);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == 255) {
                if (data != null) {
                    String lat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0) + "";
                    String lon = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0) + "";
                    String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);
                    viewModel.entity.setLatitude(lat + "");
                    viewModel.entity.setLongitude(lon + "");
                    binding.address.setContent(address);
                    viewModel.entity.setTaskAddr(address);
                }
            }
        }
    }


    //分类
    private void showClassTypePop() {
        if (classDatas == null || classDatas.size() == 0 || typeDatas == null || typeDatas.size() == 0) {
            MToast.showToast(getActivity(), "类型获取失败");
            return;
        }
        OptionsPickerView businessOption = new OptionsPickerBuilder(getActivity(), (options1, options2, options3, v) -> {
            String className = classDatas.get(options1).getProjectscale();
            viewModel.entity.setFirstCategoryNo(classDatas.get(options1).getNo());
            binding.itvType.setContent(className);

            if (typeDatas.size() >= 0 && null != typeDatas.get(options1) && typeDatas.get(options1).size() > 0) {
                String typeName = typeDatas.get(options1).get(options2).getProjectscale();
                viewModel.entity.setSecondCategoryNo(typeDatas.get(options1).get(options2).getNo());
                binding.itvType.setContent(className + "/" + typeName);
                secondCategoryName = typeName;
            }
            setDefaultTitle();
        }).build();
        businessOption.setPicker(classDatas, typeDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }


    //使用时间
    private void showCourseTimePop() {
        Calendar dayCalendar = Calendar.getInstance();
        dayCalendar.setTime(new Date());
        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);

        TimePickerView timePicker = new TimePickerBuilder(getActivity(), (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            try {
                binding.timeCourse.setContent(format);
                viewModel.entity.setUseAndEnterTime(DateUtils.dateTo13Timestamp(format + " 23:59:59") + "");
                selectedDate2.setTime(date);
                binding.timeDeadline.setContent("");//清空报价截止时间
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate2);
    }

    //报价截止时间
    private void showLimitTimePop() {
        TimePickerView timePicker = new TimePickerBuilder(getActivity(), (date, v) -> {
            try {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String format = sdf.format(date);
                if (checkDate(format, binding.timeCourse.getContent())) {
                    return;
                }
                viewModel.entity.setEndTime(DateUtils.dateTo13Timestamp3(format + " 23:59:59") + "");
                binding.timeDeadline.setContent(format);
                selectedDate.setTime(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(Calendar.getInstance(), null)
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }

    //付款方式选择
    private void showPaymentMethodPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(getActivity(), (options1, options2, options3, v) -> {
            String content = allpayway.get(options1).getName();
            viewModel.entity.setRentalPayMethod(allpayway.get(options1).getId());
            viewModel.entity.setRentalPayMethodName(allpayway.get(options1).getPickerViewText());
            binding.paymentMethod.setContent(content);
        }).build();
        businessOption.setPicker(allpayway);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    private void showDriverPicker() {
        if (null != driverDatas && driverDatas.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(getActivity(), (options1, options2, options3, v) -> {
                String content = driverDatas.get(options1).getProjectscale();
                binding.itemTvDriver.setContent(content);
                viewModel.entity.setHasOperator(driverDatas.get(options1).getNo());
            }).build();
            businessOption.setPicker(driverDatas);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }


    //日期比较
    private boolean checkDate(String startTime, String endTime) {
        try {
            if (DateUtils.dateTo13Timestamp(endTime) < DateUtils.dateTo13Timestamp(startTime) || startTime.equals(endTime)) {
                MToast.showToast(getActivity(), "请选择小于使用时间的日期");
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    //动态设置设置默认标题
    private void setDefaultTitle() {
        binding.include.tvTitleDefault.setText("求租" + binding.itvBrand.getContent() + binding.itvModel.getContent() + secondCategoryName);
        binding.include.rlLeaseTitle.setVisibility(View.VISIBLE);
        binding.v.setVisibility(View.GONE);
    }

    //    选择租赁数量的单价
    private void showMatchPriceUnitPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(getActivity(), (options1, options2, options3, v) -> {
            String content = priceMatchUnitDatas.get(options1).getProjectscale();
            binding.inLeaseNum.machineryNumType.setText(content);
            viewModel.entity.setRentalCountUnit(content);
        }).build();
        businessOption.setPicker(priceMatchUnitDatas);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    /**
     * 隐藏软键盘
     */
    private void setSoftWareHide() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
    }


    //返回省市区数据加载数据
    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        addressEntityList = data;
        initJsonData();
    }

    //组装省市区数据信息
    private void initJsonData() {
        for (int i = 0; i < addressEntityList.size(); i++) {
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {
                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
    }






    /**
     * 编辑回显 使用
     *
     * @param entity
     */
    public void showDetail(AskLeaseFarmEntity entity) {
        viewModel.entity = entity;
        binding.itvType.setContent(entity.getFirstCategoryName() + "/" + entity.getSecondCategoryName());
        binding.leaseUsername.setContent(entity.getContactPerson());
        binding.itemTvPhone.setContent(entity.getContactPhone());
        binding.timeCourse.setContent(DateUtils.stampToDay(entity.getUseAndEnterTime()));
        binding.timeDeadline.setContent(DateUtils.stampToDay(entity.getEndTime()));

        binding.address.setContent(entity.getTaskAddr());
        if (!TextUtils.isEmpty(entity.getRentalPayMethodName())) {
            binding.paymentMethod.setContent(entity.getRentalPayMethodName());
        } else {
            binding.paymentMethod.setContent(entity.getRentalPayMethod());
        }
        binding.include.itemTvTitleEdit.setEditContent(entity.getTitle() + "");
        binding.leaseExpand.setEditContent(entity.getRemark());
        binding.inLeaseNum.machineryNum.setText(entity.getRentalCount());
        if (!TextUtils.isEmpty(entity.getRentalCountUnit())) {
            binding.inLeaseNum.machineryNumType.setText(entity.getRentalCountUnit());
        }
        if (!TextUtils.isEmpty(entity.getBrand())) {
            binding.itvBrand.setContent(entity.getBrand());
        }
        if (!TextUtils.isEmpty(entity.getFactoryModel())) {
            binding.itvModel.setContent(entity.getFactoryModel());
        }
        if ("0".equals(entity.getHasOperator())) {
            binding.itemTvDriver.setContent("否");
        } else {
            binding.itemTvDriver.setContent("是");
        }
        binding.includeTime.leaseTime.setText(entity.getRentalDuration());
        if(!TextUtils.isEmpty(entity.getRentalDurationUnit())){
            binding.includeTime.tvTimeUnit.setText(entity.getRentalDurationUnit());
            binding.inLeasePrice.tvPriceUnit.setText("元/" + entity.getRentalDurationUnit());
        }
        try {
            if (Double.parseDouble(entity.getRentalPrice()) >= 0) {
                binding.inLeasePrice.rbPrice.setButtonDrawable(R.mipmap.lease_pub_unselect);
                binding.inLeasePrice.rbPrice.setChecked(false);
                binding.inLeasePrice.layoutPrice.setVisibility(View.VISIBLE);
                binding.inLeasePrice.rbPrice.setText("面议");
                binding.inLeasePrice.leasePrice.setText(entity.getRentalPrice());
                binding.inLeasePrice.tvPriceUnit.setText("元/" + entity.getRentalDurationUnit());
            } else {
                binding.inLeasePrice.rbPrice.setChecked(true);
                binding.inLeasePrice.rbPrice.setButtonDrawable(R.mipmap.lease_pub_select);
                binding.inLeasePrice.layoutPrice.setVisibility(View.GONE);
                binding.inLeasePrice.rbPrice.setText("您已选择面议，再次点击退出面议");
            }
        }catch (Exception exception){
//            binding.inLeasePrice.rbPrice.setChecked(true);
//            binding.inLeasePrice.rbPrice.setButtonDrawable(R.mipmap.lease_pub_select);
//            binding.inLeasePrice.layoutPrice.setVisibility(View.GONE);
//            binding.inLeasePrice.rbPrice.setText("您已选择面议，再次点击退出面议");
        }

        //旧版本面议的会导致价格单位为空,则默认一个
//        if (TextUtils.isEmpty(binding.inLeasePrice.tvPriceUnit.getText())) {
//            binding.inLeasePrice.tvPriceUnit.setText(priceUnit.get(0).getName());
//            viewModel.entity.setPriceUnitNo(priceUnit.get(0).getNo());
//            viewModel.entity.setPriceUnitName(priceUnit.get(0).getName());
//        }

    }

    //类型集合
    public void showTypeView(List<AskLeaseItemEntity<List<AskLeaseItemEntity>>> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        for (AskLeaseItemEntity<List<AskLeaseItemEntity>> l : data) {
            PickSelectEntity entity = new PickSelectEntity();
            entity.setNo(l.getNo());
            entity.setProjectscale(l.getName());
            classDatas.add(entity);
            List<PickSelectEntity> childList = new ArrayList<>();
            for (AskLeaseItemEntity ll : l.getChildren()) {
                PickSelectEntity child = new PickSelectEntity();
                child.setNo(ll.getNo());
                child.setProjectscale(ll.getName());
                childList.add(child);
            }
            typeDatas.add(childList);
        }
    }

    // 付款方式
    public void showAllPayWay(List<DictEntity3> data) {
        this.allpayway = data;
    }


    //校验数据
    private boolean checkInput() {
        if (!TextUtils.isEmpty(binding.itemTvPhone.getContent())) {
        if (!CheckUtils.isPhoneNumberValid(binding.itemTvPhone.getContent().trim())) {
            MToast.showToast(getActivity(), "请填写正确的联系电话");
            return false;
        }}
        if (TextUtils.isEmpty(binding.itvType.getContent())) {
            MToast.showToast(getActivity(), "请选择农机分类");
            return false;
        }

        if (TextUtils.isEmpty(binding.address.getContent())) {
            MToast.showToast(getActivity(), "请选择工作地");
            return false;
        }

        if(!TextUtils.isEmpty(binding.timeDeadline.getContent())&&!TextUtils.isEmpty(binding.timeCourse.getContent())) {
            if (checkDate(binding.timeDeadline.getContent(), binding.timeCourse.getContent())) {
                return false;
            }
        }

        viewModel.entity.setRentalDuration(binding.includeTime.leaseTime.getText().toString());

        if (binding.inLeasePrice.rbPrice.isChecked()) {
            viewModel.entity.setRentalPrice("-1");
        } else {
            String price = binding.inLeasePrice.leasePrice.getText().toString().trim();
            if (!TextUtils.isEmpty(price)) {
                if (Double.parseDouble(price) <= 0) {
                    MToast.showToast(getActivity(), "请填写大于0的期望租金");
                    return false;
                }
            }
            viewModel.entity.setRentalPrice(price);
        }

        viewModel.entity.setBrand(binding.itvBrand.getContent());
        viewModel.entity.setFactoryModel(binding.itvModel.getContent());
        viewModel.entity.setRentalCount(binding.inLeaseNum.machineryNum.getText().toString());
        viewModel.entity.setTaskAddr(binding.address.getContent());
        viewModel.entity.setContactPerson(binding.leaseUsername.getContent());
        viewModel.entity.setContactPhone(binding.itemTvPhone.getContent());
        viewModel.entity.setRemark(binding.leaseExpand.getEditContent());
        //设置标题参数
        if (!TextUtils.isEmpty(binding.include.itemTvTitleEdit.getEditContent())) {
            viewModel.entity.setTitle(binding.include.itemTvTitleEdit.getEditContent());
        } else {
            MToast.showToast(getActivity(), "请填写业务名称");
            return false;
        }
        return true;
    }


    /**
     * 租赁时长
     */
    private void showLeaseTime() {
        leaseTimeOpv = new OptionsPickerBuilder(getActivity(), (options1, options2, options3, v) -> {
            String content = leaseTimeDatas.get(options1).getProjectscale();
            String no = leaseTimeDatas.get(options1).getNo();
            binding.includeTime.tvTimeUnit.setText(content);
            viewModel.entity.setRentalDurationUnit(content);
            viewModel.entity.setRentalDurationUnitNo(no);
            binding.inLeasePrice.tvPriceUnit.setText("元/" + content);
        }).build();
        leaseTimeOpv.setPicker(leaseTimeDatas);
        leaseTimeOpv.setSelectOptions(0);
        if (leaseTimeDatas.size() > 0) {
            String unit = leaseTimeDatas.get(0).getProjectscale();
            viewModel.entity.setRentalDurationUnit(unit);
            viewModel.entity.setRentalDurationUnitNo(leaseTimeDatas.get(0).getNo());
            binding.includeTime.tvTimeUnit.setText(unit);
            binding.inLeasePrice.tvPriceUnit.setText("元/" + unit);
        }
    }
}
